package com.umeng.soexample.mvp.view.fragment;

import android.database.Cursor;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.CouponListEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerCouponListFragmentCompont;
import com.umeng.soexample.di.modules.CouponListFragmentModules;
import com.umeng.soexample.mvp.contract.CouponListFragmentContract;
import com.umeng.soexample.mvp.persenter.CouponListFragmentPersenter;
import com.umeng.soexample.mvp.view.adapter.CouponListViewPagerAdapter;
import com.umeng.soexample.mvp.view.adapter.CouponListWSYAdapter;
import com.umeng.soexample.mvp.view.custom.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class CouponListFragment extends BaseFragment<CouponListFragmentPersenter> implements CouponListFragmentContract.CouponListFragmentIView ,View.OnClickListener{
    private View view;
    private RadioGroup rg_coupon_list;
    private NoScrollViewPager nsvp_coupon_list;
    private CouponListViewPagerAdapter adapter;
    private List<View> list = new ArrayList<>();
    //TODO 有货优惠券
    private View view_yoho;
    private RadioGroup rg_item_coupon_list_vp_yoho;
    private RadioButton rb_item_coupon_list_vp_yoho_wsy;
    private ViewPager vp_item_coupon_list_vp_yoho;
    private CouponListViewPagerAdapter adapter_yoho;
    private List<View> list_yoho = new ArrayList<>();
    //未使用
    private View view_yoho_wsy;
    private EditText et_item_coupon_list_vp_yoho_vp_wsy;
    private Button btn_item_coupon_list_vp_yoho_vp_wsy;
    private RecyclerView rv_item_coupon_list_vp_yoho_vp_wsy;
    private CouponListWSYAdapter yoho_wsyAdapter;

    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_coupon_list, null);
        return view;
    }

    @Override
    public void init(View view) {
        rg_coupon_list = view.findViewById(R.id.rg_coupon_list);
        nsvp_coupon_list = view.findViewById(R.id.nsvp_coupon_list);
        //TODO 有货优惠券
        view_yoho = View.inflate(getContext(), R.layout.item_coupon_list_vp_yoho, null);
        rg_item_coupon_list_vp_yoho = view_yoho.findViewById(R.id.rg_item_coupon_list_vp_yoho);
        rb_item_coupon_list_vp_yoho_wsy = view_yoho.findViewById(R.id.rb_item_coupon_list_vp_yoho_wsy);
        vp_item_coupon_list_vp_yoho = view_yoho.findViewById(R.id.vp_item_coupon_list_vp_yoho);
        //未使用布局界面
        view_yoho_wsy = View.inflate(getContext(), R.layout.item_coupon_list_vp_yoho_wsy, null);
        et_item_coupon_list_vp_yoho_vp_wsy = view_yoho_wsy.findViewById(R.id.et_item_coupon_list_vp_yoho_vp_wsy);
        btn_item_coupon_list_vp_yoho_vp_wsy= view_yoho_wsy.findViewById(R.id.btn_item_coupon_list_vp_yoho_vp_wsy);
        rv_item_coupon_list_vp_yoho_vp_wsy = view_yoho_wsy.findViewById(R.id.rv_item_coupon_list_vp_yoho_vp_wsy);
        yoho_wsyAdapter = new CouponListWSYAdapter(R.layout.item_coupon_list_vp_yoho_vp_wsy_item);
        rv_item_coupon_list_vp_yoho_vp_wsy.setAdapter(yoho_wsyAdapter);
        rv_item_coupon_list_vp_yoho_vp_wsy.setLayoutManager(new LinearLayoutManager(getContext()));

        list_yoho.add(view_yoho_wsy);
        list_yoho.add(View.inflate(getContext(), R.layout.item_coupon_list_vp_yoho_else, null));
        list_yoho.add(View.inflate(getContext(), R.layout.item_coupon_list_vp_yoho_else, null));
        adapter_yoho = new CouponListViewPagerAdapter(list_yoho);
        vp_item_coupon_list_vp_yoho.setAdapter(adapter_yoho);
        list.add(view_yoho);
        //TODO ufo优惠券
        list.add(View.inflate(getContext(), R.layout.item_coupon_list_vp_yoho_else, null));

        adapter = new CouponListViewPagerAdapter(list);
        nsvp_coupon_list.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Cursor user = USerService.getInstance().query("user", null, null);
        if (user.moveToNext()) {
            p.request(user.getString(user.getColumnIndex("user_id")));
        }
    }

    @Override
    public void setupDagger() {
        DaggerCouponListFragmentCompont.builder()
                .couponListFragmentModules(new CouponListFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        view.findViewById(R.id.iv_coupon_list_back).setOnClickListener(this);//返回上一页
        //主RadioGroup
        rg_coupon_list.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_coupon_list_yoho:
                        nsvp_coupon_list.setCurrentItem(0);
                        break;
                    case R.id.rb_coupon_list_ufo:
                        nsvp_coupon_list.setCurrentItem(1);
                        break;
                }
            }
        });
        rg_coupon_list.check(R.id.rb_coupon_list_yoho);
        //有货RadioGroup
        rg_item_coupon_list_vp_yoho.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_coupon_list_vp_yoho_wsy:
                        vp_item_coupon_list_vp_yoho.setCurrentItem(0);
                        break;
                    case R.id.rb_item_coupon_list_vp_yoho_ysy:
                        vp_item_coupon_list_vp_yoho.setCurrentItem(1);
                        break;
                    case R.id.rb_item_coupon_list_vp_yoho_ysx:
                        vp_item_coupon_list_vp_yoho.setCurrentItem(2);
                        break;
                }
            }
        });
        rg_item_coupon_list_vp_yoho.check(R.id.rb_item_coupon_list_vp_yoho_wsy);
        vp_item_coupon_list_vp_yoho.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        rg_item_coupon_list_vp_yoho.check(R.id.rb_item_coupon_list_vp_yoho_wsy);
                        break;
                    case 1:
                        rg_item_coupon_list_vp_yoho.check(R.id.rb_item_coupon_list_vp_yoho_ysy);
                        break;
                    case 2:
                        rg_item_coupon_list_vp_yoho.check(R.id.rb_item_coupon_list_vp_yoho_ysx);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        //有货 -> 未使用 -> 兑换按钮
        btn_item_coupon_list_vp_yoho_vp_wsy.setOnClickListener(this);
        et_item_coupon_list_vp_yoho_vp_wsy.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    btn_item_coupon_list_vp_yoho_vp_wsy.setBackgroundResource(R.drawable.paydone_success_black_bg);
                }else{
                    btn_item_coupon_list_vp_yoho_vp_wsy.setBackgroundResource(R.drawable.js_common_images_btnsemptyp);
                }
            }
        });
        //有货 -> 未使用 -> item立即使用
        yoho_wsyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.iv_item_coupon_list_vp_yoho_vp_wsy_item_ljsy:
                        Toast.makeText(getActivity(), "立即使用 -> " + yoho_wsyAdapter.getData().get(position).coupon_id, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

    }

    @Override
    public void success(CouponListEntity entity) {
        List<CouponListEntity.ValuesBean> valuesBeans_yoho = new ArrayList<>();
        List<CouponListEntity.ValuesBean> valuesBeans_ufo = new ArrayList<>();
        for (CouponListEntity.ValuesBean valuesBean : entity.values){
            if ("1".equals(valuesBean.coupon_ufo)){
                valuesBeans_ufo.add(valuesBean);
            }else{
                valuesBeans_yoho.add(valuesBean);
            }
        }
        rb_item_coupon_list_vp_yoho_wsy.setText("未使用(" + valuesBeans_yoho.size() + ")ˇ");
        yoho_wsyAdapter.setNewData(valuesBeans_yoho);

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_coupon_list_back://返回
                getActivity().finish();
                break;
            case R.id.btn_item_coupon_list_vp_yoho_vp_wsy://未使用 -> 兑换按钮
                Toast.makeText(getContext(), "未使用 -> 兑换按钮", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
